package LexParser;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;
/**
 * Reads mystem results from file
 *
 */
public class MystemDictionary {
	
	/**
	 * the string corresponds to the MystemWords list of objects, 
	 * each of them corresponds to the particular meaning of the word
	 * @see MystemWord
	 */
	private HashMap<String, Vector<MystemWord>> _mystemDictionary = new HashMap<String, Vector<MystemWord>>();
	
	/**
	 * creates hash map _mystemDictionary
	 */
	public void readMystemDictionary(String path) {
		FileInputStream is;
		try {
			BufferedReader bufr = new BufferedReader(new InputStreamReader
    				(new FileInputStream(
    	    				path), "UTF-8"));
			String line1 = bufr.readLine();
			while (line1 != null) {
				String line = line1.toLowerCase();
				line.replaceAll("\\?", ",");
				System.out.println("line after replace: " + line);
				String wordString = line.substring(0, line.indexOf('{'));
				String props = line.substring(line.indexOf('{') + 1, line.indexOf('}'));
				props = props.replace('=', ',');
				String[] meanings = props.split("\\|");
				Vector<MystemWord> values = new Vector<MystemWord>();
				for (String mean : meanings) {
					String[] ps = mean.split(",");
					Set<String> properties = new HashSet<String>();
					String lemma = ps[0];
					for (int i = 1; i < ps.length; i++) {
						properties.add(ps[i]);
					}
					MystemWord mw = new MystemWord(lemma, properties);
					values.add(mw);
				}
				_mystemDictionary.put(wordString, values);
				line1 = bufr.readLine();
			}
			bufr.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void print() {
		for (String wordString : _mystemDictionary.keySet()) {

			System.out.println("\nWORD:     " + wordString);
			for(MystemWord v : _mystemDictionary.get(wordString)) {
				System.out.print("\n  Value:");
				System.out.print("\n		lemma: " + v.getLemma() + ", props: ");
				for (String p : v.getProprties()) {
					System.out.print("  " + p + ",");
				}
			}
		}
	}

	/**
	 * returns the list of MystemWord objects correspondent given string
	 * @param string word from sentence
	 * @return list of MystemWord objects
	 * @see MystemWord
	 */
	public List<MystemWord> getWords(String string) {
		return _mystemDictionary.get(string);
	}
}
